<?php

include "lib/start.php";
//session_start();
//check_session();

ini_set("display_errors", "1");

$ff_open = $_GET['f'];
$file = basename($ff_open);

if(file_exists("./download/".$ff_open)){
	// registro il download nel db
	$id_type = $_REQUEST['dt'];
	$id = $_REQUEST['id'];
	$ip = $_SERVER['REMOTE_ADDR'];
	$ins = "INSERT INTO downloads (doc_id, doc_type, ip_address, data_dw) VALUES ($id, $id_type, '".$ip."', NOW())";
	$rs = $db->executeUpdate($ins);
	// aggiorno il contatore 
	$table = "";
	if($id_type == 1){
		$table = "stud_works";
		$field = "id_work";
	}
	else {
		$table = "documents";
		$field = "id";
	}
	$upd = "UPDATE $table SET dw_counter = (dw_counter + 1) WHERE $field = $id";
	$rs_upd = $db->executeUpdate($upd);
	
    $fp = fopen("./download/".$ff_open, "r");
    header("Content-Type: multipart-mixed");
    header("Content-Disposition: attachment; filename=".$file);
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    fpassthru($fp);
    @fclose($fp);
    exit;
}
else{
    print("<br>...file non trovato: /download/".$ff_open."<br><br><a href=\"JavaScript: history.go(-1);\">Indietro</a>");
}
?>